/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.data.collections.Map;
import com.bourre.log.LogChannel;
import com.bourre.log.LogEvent;
import com.bourre.log.Logger;
import com.bourre.log.LogLevel;
import com.bourre.log.LogListener;

import flash.external.ExternalInterface;

/**
 * Displays log messages into Firebug console.
 * 
 * <p>Example
 * {@code
 *   Logger.getInstance().addLogListener( FireBugTracer.getInstance(), FeverDebug.channel );
 *   
 *   FeverDebug.WARN( 'A warning message' );
 * }
 * 
 * @author Romain Ecarnot
 */
class fever.utils.FireBugTracer implements LogListener
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _instance : FireBugTracer;
	private var _mFormat : Map;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Creates {@link FireBugTracer} instance and connects it to passed 
	 * {@code channel} for logging API.
	 * 
	 * <p>Example
	 * {@code 
	 *   FireBugTracer.connect( FeverDebug.channel );
	 * }
	 */
	public static function connect( channel : LogChannel ) : FireBugTracer
	{
		var o : FireBugTracer = FireBugTracer.getInstance();
		Logger.getInstance().addLogListener( o, channel );
		return o;	
	}
	
	/**
	 * Creates and returns a <strong>FireBugTracer</strong> instance.
	 */
	public static function getInstance() : FireBugTracer
	{
		if( _instance == undefined ) _instance = new FireBugTracer();
		return _instance;
	}
	
	/**
	 * Writes the passed-in {@caption} message to the console and opens 
	 * a nested block to indent all future messages sent to the console.
	 */
	public function openGroup( caption : String ) : Void
	{
		var title : String = ( caption ) ? caption : 'new group';
		ExternalInterface.call( 'console.group', title );
	}
	
	/**
	 * Closes the most recently opened block created by a call to 
	 * {@link #openGroup}.
	 */
	public function closeGroup( ) : Void
	{
		ExternalInterface.call( 'console.groupEnd' );
	}
	
	/**
	 * Sends log message obtained from the passed-in {@code e} model to all
	 * registred {@code Logger} listeners.
	 *
	 * @param e Pixlib LogEvent instance
	 */
	public function onLog( e : LogEvent ) : Void
	{
		var methodName : String = _mFormat.get( e.level );
		ExternalInterface.call( methodName, e.content.toString() );
	}
	
	/**
	 * Logs message throw FireBug console.<br />
	 * Does not need <strong>Pixlib Loggin API</strong>.
	 * 
	 * @param o Log message
	 */
	public function log( o ) : Void
	{
		var methodName : String = _mFormat.get( LogLevel.DEBUG );
		ExternalInterface.call( methodName, o.toString() );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	private function FireBugTracer()
	{
		_mFormat = new Map();
		_mFormat.put( LogLevel.DEBUG, 'console.debug' );
		_mFormat.put( LogLevel.INFO, 'console.info' );
		_mFormat.put( LogLevel.WARN, 'console.warn' );
		_mFormat.put( LogLevel.ERROR, 'console.error' );
		_mFormat.put( LogLevel.FATAL, 'console.error' );
	}
}